package com.bfly.mall.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.bfly.common.ResponseUtil;
import com.bfly.common.util.DateUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.order.entity.Order;
import com.bfly.mall.order.enums.ExpressStatusEnum;
import com.bfly.mall.order.enums.OrderStatusEnum;
import com.bfly.mall.order.enums.PayStatusEnum;
import com.bfly.mall.order.service.IOrderService;
import com.bfly.mall.report.dto.DataValueDTO;
import com.bfly.mall.report.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/10/16 20:46
 */
@RestController
@RequestMapping(value = "/manage/report/order")
public class OrderReportController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IReportService reportService;

    /**
     * 订单统计
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/4 12:45
     */
    @GetMapping(value = "/statistics")
    @PermissionCheck(required = false)
    public void statisticsOrder(HttpServletResponse response) {
        JSONObject json = new JSONObject();
        JSONObject orderAllJson = statisticsOrder();
        JSONObject orderToadyJson = statisticsOrderToday();
        json.putAll(orderAllJson);
        json.putAll(orderToadyJson);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(json));
    }

    /**
     * 订单概览统计
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/16 20:20
     */
    private JSONObject statisticsOrder() {
        Map<String, Object> param = new HashMap<>();

        // 订单总数量
        long total = orderService.getCount(param);

        // 待发货
        param.put("order_status", OrderStatusEnum.OPEN.getId());
        param.put("pay_status", PayStatusEnum.SUCCESS.getId());
        param.put("express_status", ExpressStatusEnum.WAIT_SHIP.getId());
        long unDeliver = orderService.getCount(param);

        // 待支付
        param.put("order_status", OrderStatusEnum.OPEN.getId());
        param.put("pay_status", PayStatusEnum.WAIT_PAY.getId());
        param.put("express_status", ExpressStatusEnum.INITIAL.getId());
        long unPay = orderService.getCount(param);

        // 已取消
        param.put("order_status", OrderStatusEnum.CANCEL.getId());
        param.put("pay_status", PayStatusEnum.WAIT_PAY.getId());
        param.put("express_status", ExpressStatusEnum.INITIAL.getId());
        long cancel = orderService.getCount(param);

        // 待收货
        param.put("order_status", OrderStatusEnum.OPEN.getId());
        param.put("pay_status", PayStatusEnum.SUCCESS.getId());
        param.put("express_status", ExpressStatusEnum.SHIPPING.getId());
        long unReceive = orderService.getCount(param);


        // 已完成
        param.put("order_status", OrderStatusEnum.COMPLETE.getId());
        param.put("pay_status", PayStatusEnum.SUCCESS.getId());
        param.put("express_status", ExpressStatusEnum.RECEIVED.getId());
        long complete = orderService.getCount(param);

        // 已关闭
        param.remove("pay_status");
        param.remove("express_status");
        param.put("order_status", OrderStatusEnum.CLOSE.getId());
        long close = orderService.getCount(param);

        JSONObject json = new JSONObject();
        json.put("total", total);
        json.put("unDeliver", unDeliver);
        json.put("unPay", unPay);
        json.put("unReceive", unReceive);
        json.put("complete", complete);
        json.put("close", close);
        json.put("cancel", cancel);
        return json;
    }

    /**
     * 今日订单综合统计
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/4 13:43
     */
    private JSONObject statisticsOrderToday() {
        Map<String, Object> param = new HashMap<>();

        Date beginDate = DateUtil.getCurrentDayMorning();
        Date endDate = DateUtil.getCurrentDayNight();

        // 今日订单数
        param.put("create_date_beginDate", beginDate);
        param.put("create_date_endDate", endDate);
        long todayTotal = orderService.getCount(param);

        // 今日销售额
        param.remove("express_status");
        param.remove("order_status");
        param.put("pay_status", PayStatusEnum.SUCCESS.getId());
        List<Order> todayOrders = orderService.getList(param);
        BigDecimal salePrice = new BigDecimal(0);
        if (todayOrders != null) {
            todayOrders.forEach(order -> {
                salePrice.add(order.getProductPrice());
            });
        }

        JSONObject json = new JSONObject();
        json.put("todaySalePrice", salePrice);
        json.put("todayTotal", todayTotal);
        return json;
    }

    /**
     * 统计用户增长趋势
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/9 12:25
     */
    @GetMapping(value = "/increase")
    public void statisticsOrderIncrease(@RequestParam("start") Date start, @RequestParam("end") Date end, HttpServletResponse response) {
        List<DataValueDTO> list = reportService.statisticsOrderIncrease(start, end);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }
}
